package com.haidechizi.tree;

import sun.reflect.generics.tree.Tree;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author: wangdejun
 * @create: 2020-03-14 15:21
 **/
public class TreeNode implements Comparable<TreeNode> {

    private int value;

    private TreeNode left;
    private TreeNode right;

    public TreeNode(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }


    public TreeNode getLeft() {
        return left;
    }

    public void setLeft(TreeNode left) {
        this.left = left;
    }

    public TreeNode getRight() {
        return right;
    }

    public void setRight(TreeNode right) {
        this.right = right;
    }


    @Override
    public int compareTo(TreeNode o) {
        return this.value - o.value;
    }

    public void frontShow() {
        System.out.println(this.value);
        if (this.left != null) {
            left.frontShow();
        }
        if (right != null) {
            right.frontShow();
        }
    }

    public void deepShow() {
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(this);
        while (!queue.isEmpty()) {
            TreeNode node = queue.poll();
            System.out.println(node.value);
            if (node.left != null) {
                queue.add(node.left);
            }
            if (node.right != null) {
                queue.add(node.right);
            }
        }
    }
}
